package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;

import entity.User;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import thread.QunFa;

public class P2PFrame2 extends JFrame{
	private JButton sendBtn;
	private JButton jitter;
	private static JTextArea ta;
	private JTextField tf;
	private JPanel mainPanel;
	
	private JScrollPane jsp;
	private JScrollPane tajsp;
	private String hisusername;//对方的username
	private String myusername;//对方的username
	private Socket socket;
	private static JTable jtbOnline;
	
	private int x;//
	private int y;//
	
	public JTextArea getTa() {
		return ta;
	}

	public static void setTa(JTextArea ta) {
		P2PFrame2.ta = ta;
	}

	public void init(){
		this.setTitle(myusername+"-to-"+hisusername);
		this.setLayout(new BorderLayout());
		ta=new JTextArea(6,20);
		tf=new JTextField();
		mainPanel=new JPanel(new BorderLayout());
		
		
	
		tajsp=new JScrollPane(ta);
		sendBtn=new JButton("send");
		jitter = new JButton("jitter");
		JPanel btnpan = new JPanel();
	
		
		mainPanel.add(tajsp, BorderLayout.NORTH);
		mainPanel.add(tf, BorderLayout.CENTER);
		mainPanel.add(btnpan, BorderLayout.SOUTH);
		btnpan.add(sendBtn, BorderLayout.WEST);
		btnpan.add(jitter, BorderLayout.EAST);
		this.add(mainPanel,BorderLayout.CENTER);
		
		
	}
	
	public void addEvent() throws Exception{
		sendBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			    
			   
			    	String msg=	tf.getText();
			    	OutputStream os;
					try {
						os = socket.getOutputStream();
						os.write("df2\n".getBytes());
						os.write((hisusername+"\n").getBytes());
						os.write((myusername+"\n").getBytes());
						os.write((myusername+":"+msg+"\n").getBytes());
						ta.append(myusername+":"+msg+ "\n");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				
					 tf.setText("");
				
			   
			}
		});
	}
	public void addEvent2() throws Exception {
		jitter.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				OutputStream os;
				try {
					os = socket.getOutputStream();
					os.write("exzd\n".getBytes());
					os.write((hisusername + "\n").getBytes());
					os.write((myusername + "\n").getBytes());				
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				tf.setText("");

			}
		});
	}
	
	public void doudong(){
		x = this.getX();
		y = this.getY();
		for (int i = 0; i < 20; i++) {
			if ((i & 1) == 0) {
				x += 3;
				y += 3;
			} else {
				x -= 3;
				y -= 3;
			}
			this.setLocation(x, y);
			try {
				Thread.sleep(100);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

	}

	
	public P2PFrame2(String hisusername,String myusername, Socket socket) throws Exception {
		// TODO Auto-generated constructor stub
		this.hisusername = hisusername;
		this.myusername = myusername;
		this.socket = socket;
		this.setSize(500, 300);
		this.setLocationRelativeTo(null);//�򿪿�����������
		init();
		addEvent();
		addEvent2();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//�رմ���
		this.setVisible(true);//��Ⱦ
	}
	
	public static void main(String[] args) {
		
	
		/*P2PFrame frame=new P2PFrame();*/
		
		
		
	}
}
